<?php

function getOrder($id, $uid)
{
	$sql = 'select * from zcshop_orders where uid=' . $uid . ' and orderid=' . $id . ' and has_shutted="0" and flid="1"';
	$result = mysql_query($sql);
	if ($result) {
		$row = mysql_fetch_array($result);
	} else {
		$row = array();
	}
	
	return $row;
}

function updateOrder($data, $id)
{
	$sql = 'update zcshop_orders set ';
	$sqlArr = array();
	foreach ($data as $k=>$v) {
		if ($k == 'payment' || $k == 'order_score_statu' || $k == 'score_pay_statu') {
			$sqlArr[] = $k . '="' . $v . '"'; 
		} else {
			$sqlArr[] = $k . '=' . $v;
		}
	}
	$sql .= implode(',', $sqlArr);
	$sql .= ' where orderid=' . $id;
	return mysql_query($sql);	
}

function updateIntegral($dollar, $uid)
{
	$statu = orderScore('statu');
	if ($statu == 1) {
		$orderDollar = getConfig('ORDER_DOLLAR');	
		
		$dollar = $dollar / $orderDollar['data'];
		$score = dollar2score($dollar);
		$sql = 'update `zcshop_members` set integral=integral+' . $score . ' where uid=' . $uid;	
		return mysql_query($sql);
	}
	return true;
}

function getConfig($name)
{
	$sql = 'select * from `zcshop_configs` where name="' . $name . '"';
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	return $row;
}

function orderScore($type = 'score')
{
	$row = getConfig('ORDER_SCORE');
	$scoreArr = explode('|', $row['data']);
	if ($type == 'score') {
		return $scoreArr[0];
	} else {
		return $scoreArr[1];
	}
}

function dollar2score($dollar)
{
	$score = orderScore('score');
	$scoreResult = $dollar * $score;
	return $scoreResult;
}


function addConfirm($data)
{
	$keyArr = array();
	$valArr = array();
	$sql = 'insert into `zcshop_confirmations` (';
	foreach ($data as $k=>$v) {
		$keyArr[] = $k;
		if ($k == 'uid' || $k == 'add_time' || $k == 'total' || $k == 'paid_total') {
			$valArr[] = $v;
		} else {
			$valArr[] = '"' . addslashes($v) . '"';
		}
	}
	$sql .= implode(',', $keyArr) . ') values (';
	$sql .= implode(',', $valArr) . ')';
	return mysql_query($sql);
}

function getAccount($id)
{
	$sql = 'select * from `zcshop_accounts` where sign=' . $id;
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	return $row;
}

function addAccount($data)
{
	$keyArr = array();
	$valArr = array();
	$sql = 'insert into `zcshop_accounts` (';
	foreach ($data as $k=>$v) {
		$keyArr[] = $k;
		if ($k == 'orderid_string' || $k == 'payment' || $k == 'remark') {
			$valArr[] = '"' . addslashes($v) . '"';
		} else {
			$valArr[] = $v;
		}
	}
	$sql .= implode(',', $keyArr) . ') values (';
	$sql .= implode(',', $valArr) . ')';
	return mysql_query($sql);
}

function updateAccount($data, $id)
{
	$sql = 'update `zcshop_accounts` set ';
	$sqlArr = array();
	foreach ($data as $k=>$v) {
		if ($k == 'orderid_string' || $k == 'payment' || $k == 'remark') {
			$sqlArr[] = $k . '="' . $v . '"'; 
		} else {
			$sqlArr[] = $k . '=' . $v;
		}
	}
	$sql .= implode(',', $sqlArr);
	$sql .= ' where accountid=' . $id;	
	return mysql_query($sql);	
}